Certified Java Programmer Mock Exam


Question 1


class B {
  private String name;
  public B(String s) {name = s;}
  public void finalize() {System.out.print(name);}
}
class E {
  public static void m() {
    B x1 = new B("X");
    B y1 = new B("Y");
  }
  public static void main(String[] args) {
    m();
    System.gc();
  }
}


Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?

a. Prints: XY
b. Prints: YX
c. Prints: XXYY
d. Prints: YYXX
e. Nothing is printed.
f. There is no guarantee that the garbage collector will run.
g. Compiler Error.
h. Run Time Error.
i. None of the above.

Question 2


class I {
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private void m1() {
    I i1 = new I();
    I i2 = new I();
    I i3 = new I();
    i1.other(i3);
    i2.other(i1);
    i3.other(i2);
  }
  public static void main (String[] args) {
    new J().m1();
  }
}

Which objects are eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. None.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 3


class I {
  private String name;
  public String toString() {return name;}
  public I(String s) {name = s;}
}
class J {
  private static void m1(I[] a1) {
    a1[0] = a1[1] = a1[2] = null;
  }
  public static void main (String[] args) {
    I[] a1 = new I[3];   // 1
    a1[0] = new I("A");  // 2
    a1[1] = new I("B");  // 3
    a1[2] = new I("C");  // 4
    m1(a1);
    for (int i = 0; i < a1.length; i++) {
      System.out.print(a1[i]);
    }
  }
}

After method m1 returns the objects on which of the following lines are eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 4


class I {
  private String name;
  public I(String s) {name = s;}
  private I other;
  public void other(I i) {other = i;}
}
class J {
  private I i1 = new I("A");
  private I i2 = new I("B");
  private I i3 = new I("C");
  private void m1() {
    i1.other(i2);
    i2.other(i1);
    i3.other(i3);
    i1 = i3;
    i2 = i3;
    m2();
  }
  private void m2() {
    // Do amazing things.
  }
  public static void main (String[] args) {
    new J().m1();
  }
}

Which of the three objects, A, B, C, are eligible for garbage collection when method m2 begins to execute?

a. A
b. B
c. C
d. None.
e. Compiler error.
f. Run time error.
g. None of the above.

Question 5


void m1() {
  Q q1 = null;
  for (int i = 0; i < 10; i++) {
    q1 = new Q();   // 1
    m2(q1);         // 2
  }
  System.out.print("All done"); // 3
}

When the processing of line 3 begins how many objects of type Q that were created at line 1 are eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 6


class I {
  private String name;
  public String toString() {return name;}
  public I(String s) {name = s;}
}
class J {
  private static void m1(I[] a1) {
    a1 = null;
  }
  public static void main (String[] args) {
    I[] a1 = new I[3];   // 1
    a1[0] = new I("A");  // 2
    a1[1] = new I("B");  // 3
    a1[2] = new I("C");  // 4
    m1(a1);
    for (int i = 0; i < a1.length; i++) {
      System.out.print(a1[i]);
    }
  }
}

After method m1 returns the objects on which of the following lines are eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 7


void m1() {
  Q q1 = null;
  for (int i = 0; i < 10; i++) {
    q1 = new Q();               // 1
  }
  System.out.print("All done"); // 2
}

When the processing of line 2 begins how many objects of type Q that were created at line 1 are eligible for garbage collection?

a. 0
b. 1
c. 9
d. 10
e. Indeterminate.
f. Compiler error.
g. Run time error.
h. None of the above.

Question 8


class I {
  private String name;
  public String name() {return name;}
  public I(String s) {name = s;}
}

class J {
  public static void m1(I i) {i = null;}
  public static void main (String[] args) {
    I i = new I("X");           // 1
    m1(i);                      // 2
    System.out.print(i.name()); // 3
  }
}

Which of the following are true statements and which of the following could be a result of attempting to compile and run the program?

a. The object created a line 1 is eligible for garbage collection after line 2.
b. A NullPointerException is generated at line 3.
c. Prints X.
d. Compiler error.
e. None of the above.


Copyright © 2002, Dan Chisholm
All rights reserved.
1